package com.example.musicplayer.bean;

import java.util.ArrayList;
import java.util.List;

import com.example.musicplayer.common.MusicConstant;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.provider.MediaStore.Audio.Media;

public class MusicAdapter {

    private static final String _ID = Media._ID;
    private static final String DATA = Media.DATA;
    private static final String SIZE = Media.SIZE;
    private static final String DISPLAY_NAME = Media.DISPLAY_NAME;
    private static final String TITLE = Media.TITLE;
    private static final String DATE_ADDED = Media.DATE_ADDED;
    private static final String DATE_MODIFIED = Media.DATE_MODIFIED;
    private static final String MIME_TYPE = Media.MIME_TYPE;
    private static final String TITLE_KEY = Media.TITLE_KEY;
    private static final String DURATION = Media.DURATION;
    private static final String BOOKMARK = Media.BOOKMARK;
    private static final String ARTIST_ID = Media.ARTIST_ID;
    private static final String ARTIST = Media.ARTIST;
    private static final String ARTIST_KEY = Media.ARTIST_KEY;
    private static final String COMPOSER = Media.COMPOSER;
    private static final String ALBUM_ID = Media.ALBUM_ID;
    private static final String ALBUM = Media.ALBUM;
    private static final String ALBUM_KEY = Media.ALBUM_KEY;
    private static final String TRACK = Media.TRACK;
    private static final String YEAR = Media.YEAR;
    private static final String IS_MUSIC = Media.IS_MUSIC;
    private static final String IS_PODCAST = Media.IS_PODCAST;
    private static final String IS_RINGTONE = Media.IS_RINGTONE;
    private static final String IS_ALARM = Media.IS_ALARM;
    private static final String IS_NOTIFICATION = Media.IS_NOTIFICATION;

    public static String[] PROJECTION = new String[] {
            _ID,
            DATA,
            SIZE,
            DISPLAY_NAME,
            TITLE,
            DATE_ADDED,
            DATE_MODIFIED,
            MIME_TYPE,
            TITLE_KEY,
            DURATION,
            BOOKMARK,
            ARTIST_ID,
            ARTIST,
            ARTIST_KEY,
            COMPOSER,
            ALBUM_ID,
            ALBUM,
            ALBUM_KEY,
            TRACK,
            YEAR,
            IS_MUSIC,
            IS_PODCAST,
            IS_RINGTONE,
            IS_ALARM,
            IS_NOTIFICATION
    };

    public List<Music> getMusics(Context context) {
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = null;
        List<Music> musics = null;

        try {
            cursor = resolver.query(MusicConstant.MEDIA_EXTERNAL_CONTENT_URI, PROJECTION, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                musics = new ArrayList<Music>();
                Music music = null;
                while (cursor.moveToNext()) {
                    music = getMusic(cursor);
                    musics.add(music);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

        return musics;
    }

    private static Music getMusic(Cursor cursor) {
        if (cursor == null) {
            return null;
        }

        Music music = new Music();
        music.setId(cursor.getLong(cursor.getColumnIndex(_ID)));
        music.setData(cursor.getBlob(cursor.getColumnIndex(DATA)));
        music.setSize(cursor.getLong(cursor.getColumnIndex(SIZE)));
        music.setDisplayName(cursor.getString(cursor.getColumnIndex(DISPLAY_NAME)));
        music.setTitle(cursor.getString(cursor.getColumnIndex(TITLE)));
        music.setDateAdded(cursor.getLong(cursor.getColumnIndex(DATE_ADDED)));
        music.setDateModified(cursor.getLong(cursor.getColumnIndex(DATE_MODIFIED)));
        music.setMimeType(cursor.getString(cursor.getColumnIndex(MIME_TYPE)));
        music.setTitleKey(cursor.getString(cursor.getColumnIndex(TITLE_KEY)));
        music.setDuration(cursor.getLong(cursor.getColumnIndex(DURATION)));
        music.setBookmark(cursor.getLong(cursor.getColumnIndex(BOOKMARK)));
        music.setArtistId(cursor.getLong(cursor.getColumnIndex(ARTIST_ID)));
        music.setArtist(cursor.getString(cursor.getColumnIndex(ARTIST)));
        music.setArtistKey(cursor.getString(cursor.getColumnIndex(ARTIST_KEY)));
        music.setComposer(cursor.getString(cursor.getColumnIndex(COMPOSER)));
        music.setAlbumId(cursor.getLong(cursor.getColumnIndex(ALBUM_ID)));
        music.setAlbum(cursor.getString(cursor.getColumnIndex(ALBUM)));
        music.setAlbumKey(cursor.getString(cursor.getColumnIndex(ALBUM_KEY)));
        music.setTrack(cursor.getString(cursor.getColumnIndex(TRACK)));
        music.setYear(cursor.getInt(cursor.getColumnIndex(YEAR)));
        music.setIsMusic(cursor.getInt(cursor.getColumnIndex(IS_MUSIC)));
        music.setIsPodcast(cursor.getInt(cursor.getColumnIndex(IS_PODCAST)));
        music.setIsRingTone(cursor.getInt(cursor.getColumnIndex(IS_RINGTONE)));
        music.setIsAlarm(cursor.getInt(cursor.getColumnIndex(IS_ALARM)));
        music.setIsNotification(cursor.getInt(cursor.getColumnIndex(IS_NOTIFICATION)));

        return music;
    }
}
